#!/bin/bash

USER_ADM="uollogs"
PASS_ADM="misslog"

MAQUINA1="a1-zencoder1.host.intranet"
MAQUINA2="a1-zencoder2.host.intranet"
MAQUINA3="a2-zencoder1.host.intranet"
MAQUINA4="a2-zencoder2.host.intranet"

ENCODERS1="1 2 3 4 5 6 7 8 9"
ENCODERS2="11 12 13 14 15 16 17 18 19"
ENCODERS3="21 22 23 24 25 26 27 28 29"
ENCODERS4="31 32 33 34 35 36 37"

EMAIL="l-tec-alta-audiencia nnozu l-tec-moni"

for i in ${MAQUINA1}; do
	for j in ${ENCODERS1}; do
		/usr/bin/wget http://${USER_ADM}:${PASS_ADM}@${i}:8765/encoder-taipei/${j}-stderr.log -O /tmp/${j}.log.mais
		CONTA=$(/bin/grep "java.lang.OutOfMemoryError: unable to create new native thread" /tmp/${j}.log.mais |/usr/bin/wc -l)
		if [ ${CONTA} != "0" ]; then
			for p in ${EMAIL}; do
				echo "encoder ${j} DOWN - Abrir IM para ADMPORTAL P3" | /bin/mail -s "Encoder DOWN" ${p}\@uolinc.com
			done
		fi
	done
done

for i in ${MAQUINA2}; do
	for j in ${ENCODERS2}; do
		/usr/bin/wget http://${USER_ADM}:${PASS_ADM}@${i}:8765/encoder-taipei/${j}-stderr.log -O /tmp/${j}.log.mais
		CONTA=$(/bin/grep "java.lang.OutOfMemoryError: unable to create new native thread" /tmp/${j}.log.mais |/usr/bin/wc -l)
		if [ ${CONTA} != "0" ]; then
			for p in ${EMAIL}; do
				echo "encoder ${j} DOWN - Abrir IM para ADMPORTAL P3" | /bin/mail -s "Encoder DOWN" ${p}\@uolinc.com
			done
		fi
	done
done

for i in ${MAQUINA3}; do
	for j in ${ENCODERS3}; do
		/usr/bin/wget http://${USER_ADM}:${PASS_ADM}@${i}:8765/encoder-taipei/${j}-stderr.log -O /tmp/${j}.log.mais
		CONTA=$(/bin/grep "java.lang.OutOfMemoryError: unable to create new native thread" /tmp/${j}.log.mais |/usr/bin/wc -l)
		if [ ${CONTA} != "0" ]; then
			for p in ${EMAIL}; do
				echo "encoder ${j} DOWN - Abrir IM para ADMPORTAL P3" | /bin/mail -s "Encoder DOWN" ${p}\@uolinc.com
			done
		fi
	done
done

for i in ${MAQUINA4}; do
	for j in ${ENCODERS4}; do
		/usr/bin/wget http://${USER_ADM}:${PASS_ADM}@${i}:8765/encoder-taipei/${j}-stderr.log -O /tmp/${j}.log.mais
		CONTA=$(/bin/grep "java.lang.OutOfMemoryError: unable to create new native thread" /tmp/${j}.log.mais |/usr/bin/wc -l)
		if [ ${CONTA} != "0" ]; then
			for p in ${EMAIL}; do
				echo "encoder ${j} DOWN - Abrir IM para ADMPORTAL P3" | /bin/mail -s "Encoder DOWN" ${p}\@uolinc.com
			done
		fi
	done
done

rm /tmp/*.log.mais
